const Response = require("../../class/Response");
// 云端指令服务
WSC.onMessagePurpose(CONSTANTS.MESSAGE_PURPOSE.COMMAND_REQUEST, (wsclient, req, res)=>{
    res = Response.replyTo(req, Response.MODE.CMD_RESPONSE);

    // locatePos：定位玩家位置
    if (req.body?.metaData?.purpose == '/vanilla/cmd/locatePos'){
        let pl = mc.getPlayer(req.body?.data?.sender ?? "");
        res.body.position = { x: pl?.blockPos.x, y: pl?.blockPos.y, z: pl?.blockPos.z };
        res.setStatusCode(0);
        res.setStatusMessage('');
        wsclient.sendJson(res.json);
        return false;
    }
});


// 执行普通云端指令
WSC.onMessagePurpose(CONSTANTS.MESSAGE_PURPOSE.COMMAND_REQUEST, (wsclient, req, res)=>{
    res = Response.replyTo(req, Response.MODE.CMD_RESPONSE);
    
    let commandLine = req.json.body.commandLine;

    // 更换@s为命令执行者
    commandLine = commandLine.replace(/@s/, `"${''}"`);
    
    // 执行mc命令
    let cmdRes = mc.runcmdEx(commandLine); 
    cmdRes.success ? res.setStatusCode(0) : res.setStatusCode(-1);
    res.setStatusMessage(cmdRes.output);
    wsclient.sendJson(res.json);
});
